import * as base from "./base.js";
import instance from './request.js';
import { $, $$ } from './base.js';


window.addEventListener('load', function () {


    //跳转到发布房源页面
    if (localStorage.getItem('identity') == 2) {
        $('.add_fangyuan').style.display = 'block';
    }


    //搜索模块
    $('.nav-m').addEventListener('click', function () {
        $('.search').style.display = 'block';
        $('.mask').style.display = 'block';
        $('.masksearch').style.display = 'block';
        $('.navbox').style.display = 'none';
        $('.navbox1').style.display = 'flex';

    })

    $('.masksearch').addEventListener('click', function () {
        if ($('.search-b').style.display == 'none') {
            $('.search').style.display = 'none';
            $('.mask').style.display = 'none';
            $('.masksearch').style.display = 'none';
            $('.navbox').style.display = 'flex';
            $('.navbox1').style.display = 'none';
        }

        $('.search-b').style.display = 'none';
        $('.masksearch').style.display = 'none';
        $('.search-3 div').style.transition = 'width .3s';
        $('.search-3 div').style.width = '50px';
        $('.search-3 div span').style.display = 'none';
    })


    $('.search-3 input').addEventListener('focus', function () {
        $('.search-b').style.display = 'block';
        $('.masksearch').style.display = 'block';
        $('.search-3 div').style.width = '80px';
        $('.search-3 div span').style.display = 'block';
        $('.search-3 div').style.transition = '';
        document.addEventListener('scroll', function () {
            $('.masksearch').click();
        })
    })


    $('.mask').addEventListener('click', function () {
        if ($('.search-b').style.display === 'none') {
            $('.search').style.display = 'none';
            $('.mask').style.display = 'none';
            $('.masksearch').style.display = 'none';
            $('.navbox').style.display = 'flex';
            $('.navbox1').style.display = 'none';
        }
    })


    base.navbox();
    base.debounce(base.avatarimg());
    

    //小图标
    instance.get('/home/type')
        .then(response => {
            let searcharr = response.data.data;
            searcharr.forEach(function (value, index, array) {
                let str1 = `<li class="search_svg">
        <div class="search_li_mask"></div>
    <img alt="" class="ip9sapq dir dir-ltr"
        src="https://a0.muscache.com/pictures/c5a4f6fc-c92c-4ae8-87dd-57f1ff1b89a6.jpg">
    <span id='${searcharr[index].id}'>${searcharr[index].name}</span>
</li>`
                $('.search-b-m ul').insertAdjacentHTML('beforeend', str1);

                let str2 = `<li>
    <a href="#">
        <img class="isy7598 dir dir-ltr"
            src="https://z1.muscache.cn/pictures/carson/carson-1663841385745-b016aed0/original/5dc7cf4d-a52e-401c-9c74-7223eefecf9a.png"
            alt="" width="24" height="24">
            <span id='${searcharr[index].id}'>${searcharr[index].name}</span>
    </a>
</li>`
                $('.keywords').insertAdjacentHTML('beforeend', str2);
            })


            $$('.keywords li').forEach(function (value, index, array) {
                $$('.keywords li')[index].addEventListener('click', function () {
                    $$('.keywords li a').forEach(function (value, index1, array) {
                        $$('.keywords li a')[index1].classList.remove('keywords_current');
                    })
                    $$('.keywords li a')[index].classList.add('keywords_current');

                    let keynum = $$('.keywords li a span')[index].getAttribute('id');

                    $$('.lazybox .lazyitem').forEach(function (value, index, array) {
                        value.remove();
                    })

                    lazy(keynum);

                })
            })

        })

    $('.search-b-m').addEventListener('click', function (event) {
        const targetElement = event.target;


        if (targetElement.classList.contains('search_li_mask')) {
            if (targetElement.parentNode.classList.contains('current')) {
                targetElement.parentNode.classList.remove('current');
            } else {
                $$('.search-b-m ul li').forEach(function (value, index, array) {
                    $$('.search-b-m ul li')[index].classList.remove('current');
                })
                targetElement.parentNode.classList.add('current');
            }
        }
    })



    //固定关键栏
    document.addEventListener('scroll', function () {
        if (window.pageYOffset >= 380) {
            $('.keywords').style.position = 'fixed';
            $('.keywords').style.top = '80px';
            $('.keywords').style.zIndex = '4';
        } else {
            $('.keywords').style.position = 'absolute';
            $('.keywords').style.top = '0px';
        }
    })



    //心愿单
    //名称
    $('.wishcreate_m_t input').addEventListener('focus', function () {
        $('.wishcreate_m_name').style.transform = 'scale(0.8) translateX(-10px) translateY(-15px)';
        $('.wishcreate_m_t').style.border = '1px solid #222';
    })
    $('.wishcreate_m_t input').addEventListener('blur', function () {
        $('.wishcreate_m_t').style.border = '1px solid #b0b0b0';
        if (!($('.wishcreate_m_t input').value)) {
            $('.wishcreate_m_name').style.transform = 'scale(1) translateX(0px) translateY(0px)';
        }
    })


    //清除
    $('.wishcreate_b_l').addEventListener('click', function () {
        $('.wishcreate_m_t input').value = '';
    })


    //限制字数
    $('.wishcreate_m_t input').addEventListener('input', function () {
        const count = $('.wishcreate_m_t input').value.length;
        $('.min_num').textContent = count;

        if (count >= parseInt($('.max_num').textContent)) {
            // 如果超过最大字数限制，则截断输入
            $('.wishcreate_m_t input').value = $('.wishcreate_m_t input').value.slice(0, parseInt($('.max_num').textContent - 1));
        }
    });


    let closenum = 0;
    //关闭创建心愿单
    $('.w_close_c').addEventListener('click', function () {
        if (closenum == 1) {
            $('.wishcreate').style.display = 'none';
            $('.wishbox').style.display = 'block';
        } else {
            $('.wishmask').style.display = 'none';
            $('.wishcreate').style.display = 'none';
        }
    })

    $('.w_close').addEventListener('click', function () {
        // $('.wishbox').classList.add('reverse-animation');

        // // 等待动画结束后移除.visible和.reverse-animation类名
        // setTimeout(function () {
        //     $('.wishbox').classList.remove('visible', 'reverse-animation');
        //     $('.wishbox').style.display = 'none';
        //     $('.wishmask').style.display = 'none';
        // }, 500);

        $('.wishbox').style.display = 'none';
        $('.wishmask').style.display = 'none';
        // 如果盒子可见，则添加.reverse-animation类名，触发由大变小的动画

        closenum = 0;
    })






    let wishnum = 0;
    let shoucangarr = [];
    let uniqueArray = [];
    let deltetebox = 0;
    origin();
    function origin() {
        instance.get('/collection', {
            headers: {
                'Authorization': window.localStorage.getItem('token'),
                'Content-Type': 'application/json',
            }
        }).then(response => {
            let wisharr = response.data.data;
            deltetebox = response.data.data;


            if (wisharr.length) {
                wishnum = 1;

                wisharr.forEach(function (value, index, array) {
                    let str = `<div class="wishbox_li">
            <label for="${wisharr[index].id}">
            <input type="checkbox" name="" id="${wisharr[index].id}" class="checkbox wish_checkbox">
            <div class="wish_mask" id="${wisharr[index].id}"></div>
               <div class="wishbox_li_t">
                   <div class="wishbox_li_tn">
                   </div>
               </div>
               <div class="wishbox_li_b">
                   <div class="wishbox_li_b_t">${wisharr[index].name}</div>
                   <div class="wishbox_li_b_b"><span>${wisharr[index].count}</span>心愿单项目</div>
               </div>
               </label>
               </div>`

                    $('.wishbox_m_body').insertAdjacentHTML('beforeend', str);
                    // $('.wishcreate_ul').insertAdjacentHTML('beforeend', str);
                    let that = wisharr[index];

                    that.homes.forEach(function (value, index, array) {
                        shoucangarr.push(that.homes[index].id);
                    })
                })
            }

            uniqueArray = Array.from(new Set(shoucangarr));
            // console.log(uniqueArray);
            lazy();
            checkfun();

        })
    }



    //获取被收藏的房源
    function uniqueArr() {
        return new Promise((resolve, reject) => {
            instance.get('/collection', {
                headers: {
                    'Authorization': window.localStorage.getItem('token'),
                    'Content-Type': 'application/json',
                }
            }).then(response => {
                let wisharr = response.data.data;

                let shoucangarr = [];

                if (wisharr.length) {
                    wisharr.forEach(function (value, index, array) {
                        let that = wisharr[index];

                        that.homes.forEach(function (value, index, array) {
                            shoucangarr.push(that.homes[index].id);
                        });
                    });
                }

                let uniqueArray1 = Array.from(new Set(shoucangarr));
                uniqueArray = Array.from(new Set(shoucangarr));


                resolve({ uniqueArray: uniqueArray1, wisharr: wisharr }); // 返回结果
            }).catch(error => {
                reject(error); // 返回错误
            });
        });
    }


    //点击跳转到房源详情页
    // 为 lazyitem 父元素添加 click 事件监听器
    let detailid = 0;
    let wishid = 0;
    let wishbutarr = [];
    $('.lazybox').addEventListener('click', (function (event) {
        const targetElement = event.target;


        if (targetElement.classList.contains('lazymask')) {
            console.log(targetElement.getAttribute('id'));
            detailid = targetElement.getAttribute('id');

            localStorage.setItem('detailid', targetElement.getAttribute('id'));
            localStorage.setItem('data-lazynum', targetElement.getAttribute('data-lazynum'));


            location.href = 'fangyuan.html';

        }


        //添加到心愿单
        if (targetElement.classList.contains('lazyimg_svg')) {
            uniqueArr();
            wishid = targetElement.getAttribute('id');
            // console.log(wishid);
            if (wishnum == 0) {
                $('.wishmask').style.display = 'block';
                $('.wishcreate').style.display = 'block';
            }

            if (wishnum == 1) {
                $('.wishbox').classList.add('slidedownup');
                if (targetElement.getAttribute('data-color') == 1) {

                    uniqueArr().then(result => {
                        let uniqueArray = result.uniqueArray;
                        let wisharr = result.wisharr;
                        let delarr = [];
                        // console.log(wisharr);

                        wisharr.forEach(function (value, index, array) {
                            let that = wisharr[index];
                            that.homes.forEach(function (value, index, array) {
                                if (wishid == that.homes[index].id) {
                                    delarr.push(that.id);
                                }
                            });
                        });

                        // console.log(delarr);
                        renewal();
                        wishbutarr = Array.from(new Set(delarr));
                        deltetew(Array.from(new Set(delarr)))
                    }).catch(error => {
                        console.error(error);
                    });


                } else {
                    renewal();
                    $('.wishmask').style.display = 'block';
                    $('.wishbox').style.display = 'block';
                    // $('.wishbox').classList.add('visible');
                }
            }



        }
    })
    );



    //移出收藏
    function deltetew(delarr) {
        let checkarr = [];


        // 获取所有的复选框元素
        const checkboxes = $$('.wishbox_li input[type="checkbox"]');

        // 获取全选复选框元素
        const toggleCheckbox = $('.allcheck input');

        checkboxes.forEach(function (checkbox, index1) {
            delarr.forEach(function (value, index, array) {
                if (checkboxes[index1].id == value) {
                    checkbox.checked = true;
                    checkarr.push(checkboxes[index1].id);
                }
            })
        });


        let allChecked = true;
        checkboxes.forEach(function (checkbox) {
            if (!checkbox.checked) {
                allChecked = false;
            }
        });

        // 更新全选复选框的选中状态
        toggleCheckbox.checked = allChecked;

        $('.wishmask').style.display = 'block';
        $('.wishbox').style.display = 'block';


    }






    //控制多选框
    function allCheckboxes(obj1, obj2) {
        // 获取所有的复选框元素
        const checkboxes = $$(`${obj2} input[type="checkbox"]`);

        // 获取全选复选框元素
        const toggleCheckbox = $(`${obj1} input`);

        // 获取全选复选框的选中状态
        const isChecked = toggleCheckbox.checked;

        // 遍历所有复选框元素，并设置它们的选中状态与全选复选框一致
        checkboxes.forEach(function (checkbox) {
            checkbox.checked = isChecked;
        });
    }
    function updateCheckbox(obj1, obj2) {
        // 获取所有的复选框元素
        const checkboxes = $$(`${obj2} input[type="checkbox"]`);

        // 获取全选复选框元素
        const toggleCheckbox = $(`${obj1} input`);

        // 检查是否所有复选框都被选中
        let allChecked = true;
        checkboxes.forEach(function (checkbox) {
            if (!checkbox.checked) {
                allChecked = false;
            }
        });

        // 更新全选复选框的选中状态
        toggleCheckbox.checked = allChecked;
    }

    //收藏的
    function checkfun() {
        $('.allcheck input').addEventListener('click', function () {
            allCheckboxes('.allcheck', '.wishbox_li');
        })
        $$('.wishbox_li input[type="checkbox"]').forEach(function (value, index, array) {
            $$('.wishbox_li input[type="checkbox"]')[index].addEventListener('click', function () {
                updateCheckbox('.allcheck', '.wishbox_li');
            })
        })
    }





    //收藏的"完成"
    $('.wishbox_b_com').addEventListener('click', base.debounce(function (event) {
        wishbut();
    }));


    function wishbut() {
        console.log(wishbutarr);
        let shouyes = 0;
        let shouno = 0;
        const requestConfigs = [];
        const unrequestConfigs = [];


        $$('.wishbox_li input[type="checkbox"]').forEach(function (checkbox, index) {
            if (!(checkbox.checked) && wishbutarr.includes(parseInt($$('.wishbox_li input[type="checkbox"]')[index].id))) {
                shouyes = 1;

                const requestConfig = {
                    url: `/collection/home`,
                    method: 'DELETE',
                    data: {
                        collectionId: $$('.wishbox_li input[type="checkbox"]')[index].id,
                        homeId: wishid
                    },
                    headers: {
                        'Authorization': window.localStorage.getItem('token'),
                        'Content-Type': 'application/json',
                    }
                };

                console.log(checkbox);
                checkbox.parentNode.children[3].children[1].children[0].innerHTML--;

                requestConfigs.push(requestConfig);
            }
            if (checkbox.checked && !(wishbutarr.includes(parseInt($$('.wishbox_li input[type="checkbox"]')[index].id)))) {
                shouno = 1;

                const unrequestConfig = {
                    url: `/collection/home`,
                    method: 'POST',
                    data: {
                        collectionId: $$('.wishbox_li input[type="checkbox"]')[index].id,
                        homeId: wishid
                    },
                    headers: {
                        'Authorization': window.localStorage.getItem('token'),
                        'Content-Type': 'application/json',
                    }
                };

                console.log(checkbox);
                checkbox.parentNode.children[3].children[1].children[0].innerHTML++;

                unrequestConfigs.push(unrequestConfig);

            }
        })

        if (shouyes == 1) {
            wishbutpromise(requestConfigs);
            shouyes = 0;
        }
        if (shouno == 1) {
            wishbutpromise(unrequestConfigs);
            shouno = 0;
        }


    }



    function wishbutpromise(requestConfigs) {

        // 创建包含所有请求的 Promise 数组
        const promises = requestConfigs.map(requestConfig => instance(requestConfig));

        // 使用 Promise.all 等待所有请求完成
        Promise.all(promises)
            .then(function (responses) {
                // 处理每个请求的响应
                responses.forEach(function (response, index) {
                    console.log(`Response ${index + 1}:`, response.data);
                });



                renewal();

                //重新获取一次收藏的房源
                uniqueArr();



                $('.wishmask').style.display = 'none';
                $('.wishbox').style.display = 'none';
            }).then(response => {
                uniqueArr().then(result => {
                    let uniqueArray = result.uniqueArray;
                    let target = $$('.lazyimg svg').filter(box => box.getAttribute('id') == wishid);


                    if (uniqueArray.includes(parseInt(wishid))) {
                        target.forEach(box => {
                            box.style.fill = '#ff385c';
                            box.children[0].setAttribute('data-color', 1);
                            box.parentNode.parentNode.children[0].setAttribute('data-lazynum', 1);

                        });

                    } else {
                        target.forEach(box => {
                            box.style.fill = 'rgba(0, 0, 0, 0.5)';
                            box.children[0].setAttribute('data-color', 0);
                            box.parentNode.parentNode.children[0].setAttribute('data-lazynum', 0);
                        });
                    }

                }).catch(error => {
                    console.error(error);
                });
            })
            .catch(function (error) {
                // 处理错误
                console.error('Error:', error);
            });
    }

    //更新多选框
    function renewal() {
        const checkboxes = $$('.wishbox_li input[type="checkbox"]');

        checkboxes.forEach(function (checkbox) {
            checkbox.checked = false;
        });
        $('.allcheck input').checked = false;
    }



    //创建心愿单
    function wishcreate() {
        if ($('.wishcreate_m_t input').value) {
            instance.post('/collection', {
                name: $('.wishcreate_m_t input').value
            }, {
                headers: {
                    'Authorization': window.localStorage.getItem('token'),
                    'Content-Type': 'application/json',
                }
            }).then(response => {
                console.log(response);
                let wisharr = response.data.data;
                $('.wishcreate_m_t input').value = '';
                $('.wishmask').style.display = 'none';
                $('.wishcreate').style.display = 'none';

                let str = `<div class="wishbox_li">
        <label for="${wisharr.id}">
        <input type="checkbox" name="" id="${wisharr.id}" class="checkbox wish_checkbox">
        <div class="wish_mask" id="${wisharr.id}"></div>
           <div class="wishbox_li_t">
               <div class="wishbox_li_tn">
               </div>
           </div>
           <div class="wishbox_li_b">
               <div class="wishbox_li_b_t">${wisharr.name}</div>
               <div class="wishbox_li_b_b"><span>0</span>心愿单项目</div>
           </div>
           </label>
           </div>`

                $('.wishbox_m_body').insertAdjacentHTML('beforeend', str);

                uniqueArr();
                checkfun();

                if (wishnum == 0) {
                    origin();
                }
                $('.wishcreate_m_name').style.transform = 'scale(1) translateX(0px) translateY(0px)';

                base.tooltipBox('', '创建心愿单成功!', '1');

            }).catch(error => {
                console.error(error)
            })
        }
    }


    //创建心愿单
    $('.wishcreate_b_r').addEventListener('click', base.debounce(function () {
        wishcreate();
    }))
    $('.wishbox_b_box').addEventListener('click', function () {
        $('.wishbox').style.display = 'none';
        $('.wishcreate').style.display = 'block';
        closenum = 1;
    })






    //懒加载
    //1.获取兜底盒子，添加监听
    let observer = null;
    // lazy();
    function lazy(keynum) {
        let oTarget = document.querySelector('.loaderbody');
        if (observer) {
            observer.unobserve(oTarget);
        }
        observer = new IntersectionObserver(entries => {
            if (entries[0].intersectionRatio > 0) {
                let requestConfig = 0;
                if (keynum == 'all') {
                    requestConfig = {
                        url: `/home`,
                        method: 'GET',
                        params: {
                            offset: $$('.lazyitem').length,
                            limit: 8,
                        },
                        headers: {
                            'Authorization': window.localStorage.getItem('token'),
                            'Content-Type': 'application/json',
                        }
                    };
                } else {
                    requestConfig = {
                        url: `/home`,
                        method: 'GET',
                        params: {
                            offset: $$('.lazyitem').length,
                            limit: 8,
                            houseTypeId: keynum
                        },
                        headers: {
                            'Authorization': window.localStorage.getItem('token'),
                            'Content-Type': 'application/json',
                        }
                    };
                }

                //懒加载
                instance(requestConfig).then(response => {
                    let lazyarr = response.data.data.homeList;


                    lazyarr.forEach(function (value, index, array) {
                        let lazynum = 0;
                        let that = lazyarr[index].id;
                        uniqueArray.forEach(function (value, index, array) {
                            if (value == that) {
                                lazynum = 1;
                            }
                        })
                        if (lazynum == 1) {
                            let str = `<div class="lazyitem clearfix">
                <div class="lazymask" id="${lazyarr[index].id}" data-lazynum="${lazynum}"></div>
        <div class="lazyimg"  style="background-image:url(${lazyarr[index].pictures[0]}) ;">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" aria-hidden="true" role="presentation" focusable="false" style="display: block; fill: #ff385c; height: 24px; width: 24px; stroke: var(--f-mkcy-f); stroke-width: 2; overflow: visible;" id="${lazyarr[index].id}" ><path d="M16 28c7-4.73 14-10 14-17a6.98 6.98 0 0 0-7-7c-1.8 0-3.58.68-4.95 2.05L16 8.1l-2.05-2.05a6.98 6.98 0 0 0-9.9 0A6.98 6.98 0 0 0 2 11c0 7 7 12.27 14 17z" class="lazyimg_svg" style="stroke: white;
            stroke-width: 2px;" id="${lazyarr[index].id}" data-color="1"></path></svg>
        </div>
        <div class="lazy_bot">
            <div class="title ellipsis">${lazyarr[index].title}</div>
            <div class="introduce ellipsis">${lazyarr[index].introduce}</div>
            <div class="star">
                <span class="s1r2lfp0 dir dir-ltr"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" aria-hidden="true" role="presentation" focusable="false" style="display: block; height: 12px; width: 12px; fill: currentcolor;"><path fill-rule="evenodd" d="m15.1 1.58-4.13 8.88-9.86 1.27a1 1 0 0 0-.54 1.74l7.3 6.57-1.97 9.85a1 1 0 0 0 1.48 1.06l8.62-5 8.63 5a1 1 0 0 0 1.48-1.06l-1.97-9.85 7.3-6.57a1 1 0 0 0-.55-1.73l-9.86-1.28-4.12-8.88a1 1 0 0 0-1.82 0z"></path></svg></span>
                <span>${lazyarr[index].star ? lazyarr[index].star : '0'}</span>
            </div>
            <div class="price">￥<span>${lazyarr[index].price}</span>/每晚</div>
        </div>
    </div>`

                            $('.loaderbody').insertAdjacentHTML('beforebegin', str);

                            lazynum = 0;
                        } else {
                            let str = `<div class="lazyitem clearfix">
                <div class="lazymask" id="${lazyarr[index].id}" data-lazynum="${lazynum}"></div>
        <div class="lazyimg"  style="background-image:url(${lazyarr[index].pictures[0]}) ;">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" aria-hidden="true" role="presentation" focusable="false" style="display: block; fill: rgba(0, 0, 0, 0.5); height: 24px; width: 24px; stroke: var(--f-mkcy-f); stroke-width: 2; overflow: visible;" id="${lazyarr[index].id}"><path d="M16 28c7-4.73 14-10 14-17a6.98 6.98 0 0 0-7-7c-1.8 0-3.58.68-4.95 2.05L16 8.1l-2.05-2.05a6.98 6.98 0 0 0-9.9 0A6.98 6.98 0 0 0 2 11c0 7 7 12.27 14 17z" class="lazyimg_svg" style="stroke: white;
            stroke-width: 2px;" id="${lazyarr[index].id}" data-color="0"></path></svg>
        </div>
        <div class="lazy_bot">
            <div class="title ellipsis">${lazyarr[index].title}</div>
            <div class="introduce ellipsis">${lazyarr[index].introduce}</div>
            <div class="star">
                <span class="s1r2lfp0 dir dir-ltr"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" aria-hidden="true" role="presentation" focusable="false" style="display: block; height: 12px; width: 12px; fill: currentcolor;"><path fill-rule="evenodd" d="m15.1 1.58-4.13 8.88-9.86 1.27a1 1 0 0 0-.54 1.74l7.3 6.57-1.97 9.85a1 1 0 0 0 1.48 1.06l8.62-5 8.63 5a1 1 0 0 0 1.48-1.06l-1.97-9.85 7.3-6.57a1 1 0 0 0-.55-1.73l-9.86-1.28-4.12-8.88a1 1 0 0 0-1.82 0z"></path></svg></span>
                <span>${lazyarr[index].star ? lazyarr[index].star : '0'}</span>
            </div>
            <div class="price">￥<span>${lazyarr[index].price}</span>/每晚</div>
        </div>
    </div>`

                            $('.loaderbody').insertAdjacentHTML('beforebegin', str);
                        }



                    })


                })
            }
        })

        observer.observe(oTarget);
    }





    let fuzzyflag = 0;
    let fuzzynum = 0;
    let indexfum = 0;



    $('.search-3 div').addEventListener('click', base.debounce(function () {
        $$('.search-b-m ul li').forEach(function (value, index, array) {
            if ($$('.search-b-m ul li')[index].classList.contains('current')) {
                fuzzynum = $$('.search-b-m ul li span')[index].getAttribute('id');
                fuzzyflag = 1;
            }
        })
        if ($('.search-3 input').value == '' && fuzzyflag == 0) {
            base.tooltipBox('', '请输入内容！', '0');
        } else {

            if (indexfum == 0) {
                $('.main_lun').style.display = 'none';
                $('.keybody').style.display = 'none';
                $('.footer').style.display = 'none';
                $('.searchthing').style.display = 'block';
                indexfum = 1;
            }

            $$('.search-b-m ul li').forEach(function (value, index, array) {
                $$('.search-b-m ul li')[index].classList.remove('current');
            })



            if (fuzzyflag == 1) {

                $$('.fuzzybox').forEach(function (value, index, array) {
                    value.remove();
                })
                instance.get('/home/search', {
                    params: {
                        keyword: base.SaferHTML`${$('.search-3 input').value}`,
                        houseTypeId: fuzzynum
                    },
                    headers: {
                        'Authorization': window.localStorage.getItem('token'),
                        'Content-Type': 'application/json',
                    }
                }).then(response => {
                    console.log(base.SaferHTML`${$('.search-3 input').value}`);
                    let fuzzyarr = response.data.data.homeList;
                    console.log(fuzzyarr);
                    $('.search-3 input').value = '';
                    if (fuzzyarr != null) {
                        $('.searching_no').style.display = 'none';
                        fuzzyarr.forEach(function (value, index, array) {
                            let labindex = index;
                            let that = fuzzyarr[index];
                            let str = `<div class="fuzzybox">
                            <div class="fuzzybox_l"   style="background-image:url(${fuzzyarr[index].pictures[0]}) ;">
                                <svg viewBox="0 0 24 24" fill="#484848" fill-opacity="0.5" stroke="#ffffff" stroke-width="2" focusable="false" aria-label="添加房源到心愿单" role="img" stroke-linecap="round" stroke-linejoin="round" style="height: 28px; width: 28px; display: block; overflow: visible;"><path d="m17.5 2.9c-2.1 0-4.1 1.3-5.4 2.8-1.6-1.6-3.8-3.2-6.2-2.7-1.5.2-2.9 1.2-3.6 2.6-2.3 4.1 1 8.3 3.9 11.1 1.4 1.3 2.8 2.5 4.3 3.6.4.3 1.1.9 1.6.9s1.2-.6 1.6-.9c3.2-2.3 6.6-5.1 8.2-8.8 1.5-3.4 0-8.6-4.4-8.6" stroke-linejoin="round"></path></svg>
                            </div>
                            <div class="fuzzybox_m">
                                <div class="title ellipsis">${fuzzyarr[index].title}</div>
                                <div class="introduce ellipsis">${fuzzyarr[index].introduce}</div>
                                <div class="labels">
    
                                </div>
                            </div>
                            <div class="fuzzybox_r">
                                <div>￥<span>${fuzzyarr[index].price}</span></div>
                                <div>/晚</div>
                            </div>
                        </div>`
                            $('.searchthing_box').insertAdjacentHTML('beforeend', str);


                            if (fuzzyarr[index].labels) {
                                fuzzyarr[index].labels.forEach(function (value, index, array) {
                                    let labstr = `<div class="smalllab">${that.labels[index].name}</div>`
                                    $$('.labels')[labindex].insertAdjacentHTML('beforeend', labstr);
                                })
                            }

                        })
                    } else {
                        $('.searching_no').style.display = 'block';
                    }

                    $('.masksearch').click();
                    $('.mask').click();
                })
            } else {
                instance.get('/home/search', {
                    params: {
                        keyword: base.SaferHTML`${$('.search-3 input').value}`
                    },
                    headers: {
                        'Authorization': window.localStorage.getItem('token'),
                        'Content-Type': 'application/json',
                    }
                }).then(response => {
                    let fuzzyarr = response.data.data.homeList;
                    console.log(fuzzyarr);
                    $('.search-3 input').value = '';
                    if (fuzzyarr != null) {
                        $('.searching_no').style.display = 'none';
                        fuzzyarr.forEach(function (value, index, array) {
                            let labindex = index;
                            let that = fuzzyarr[index];
                            let str = `<div class="fuzzybox">
                            <div class="fuzzybox_l"   style="background-image:url(${fuzzyarr[index].pictures[0]}) ;">
                                <svg viewBox="0 0 24 24" fill="#484848" fill-opacity="0.5" stroke="#ffffff" stroke-width="2" focusable="false" aria-label="添加房源到心愿单" role="img" stroke-linecap="round" stroke-linejoin="round" style="height: 28px; width: 28px; display: block; overflow: visible;"><path d="m17.5 2.9c-2.1 0-4.1 1.3-5.4 2.8-1.6-1.6-3.8-3.2-6.2-2.7-1.5.2-2.9 1.2-3.6 2.6-2.3 4.1 1 8.3 3.9 11.1 1.4 1.3 2.8 2.5 4.3 3.6.4.3 1.1.9 1.6.9s1.2-.6 1.6-.9c3.2-2.3 6.6-5.1 8.2-8.8 1.5-3.4 0-8.6-4.4-8.6" stroke-linejoin="round"></path></svg>
                            </div>
                            <div class="fuzzybox_m">
                                <div class="title ellipsis">${fuzzyarr[index].title}</div>
                                <div class="introduce ellipsis">${fuzzyarr[index].introduce}</div>
                                <div class="labels">
    
                                </div>
                            </div>
                            <div class="fuzzybox_r">
                                <div>￥<span>${fuzzyarr[index].price}</span></div>
                                <div>/晚</div>
                            </div>
                        </div>`
                            $('.searchthing_box').insertAdjacentHTML('beforeend', str);


                            if (fuzzyarr[index].labels) {
                                fuzzyarr[index].labels.forEach(function (value, index, array) {
                                    let labstr = `<div class="smalllab">${that.labels[index].name}</div>`
                                    $$('.labels')[labindex].insertAdjacentHTML('beforeend', labstr);
                                })
                            }

                        })
                    } else {
                        $('.searching_no').style.display = 'block';
                    }

                    $('.masksearch').click();
                    $('.mask').click();
                })
            }
        }
    }))

    //模糊搜索的返回首页
    $('.searchthing button').addEventListener('click', function () {
        $('.main_lun').style.display = 'block';
        $('.keybody').style.display = 'block';
        $('.footer').style.display = 'block';
        $('.searchthing').style.display = 'none';

        $$('.searchthing .fuzzybox').forEach(function (value, index, array) {
            value.remove();
        })
        indexfum = 0;
    })






})